<?php

namespace App\Http\Controllers\Mobile\Company;
use App\Facades\Style;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyJob;
use App\Models\Company\CompanySearch;
use App\Repositories\Eloquent\Site;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class WelcomeController extends CommonController{

    /**
     * 手机企业中心
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function index(Request $request){
        $userInfo = auth()->guard('company')->user();
        $companyBasic=CompanyBasic::where('uid',$userInfo->uid)->orderBy('updatetime', 'desc')->first();
        //企业不存在时
        if(empty($companyBasic->id)){
           return redirect("mobile/company/register_basic");
        }
        //        更新非会员职位状态
        if (\Site::get('ordinaryJobLimit') == 1 && ($userInfo->viptime == null || $userInfo->viptime < date("Y-m-d H:i:s"))) {//将非会员职位设置成冻结中
            CompanyJob::where(['uid' => $userInfo->uid, 'job_status' => 1])->update(['job_status' => 7]);
        }else {//把冻结的职位变为公开
            CompanyJob::where(['uid' => $userInfo->uid, 'job_status' => 7])->update(['job_status' => 1]);
        }
        //会员到期日期
        $endtime=$this->timeDiff($userInfo->viptime);
        //会员积分
        $integral = $userInfo->vip_integral;
        //职位个数
        $companyJobCount=CompanyJob::where('uid',$userInfo->uid)->where('com_id',$companyBasic->id)->where('job_status','!=',4)->count();
        $companyBasic->companyJobCount =  ($companyJobCount > 0 ) ? $companyJobCount : 0 ;
        //获取面试通知记录
        $interviewCount = DB::table('interview as log')
            ->join('resume as t1', 'log.resume_id', '=', 't1.id')
            ->join('company_job as t2', 'log.job_id', '=', 't2.id')
            ->select('log.id')
            ->where('log.company_uid',$userInfo->uid)
            ->where('log.status','!=',4)
            ->count();
        $companyBasic->interviewCount =  ($interviewCount > 0 ) ? $interviewCount : 0 ;
        //部门管理
        $departmentCount = DB::table('company_department')
            ->select('id')
            ->where('uid',$userInfo->uid)
            ->count();
        $companyBasic->departmentCount =  ($departmentCount > 0 ) ? $departmentCount : 0 ;
        //搜索器
        $searchEngineCount = CompanySearch::where('uid',$userInfo->uid)->count();
        $companyBasic->searchEngineCount = ($searchEngineCount > 0 ) ? $searchEngineCount : 0 ;
        $companyBasic->phone = (!empty($companyBasic->mobile)) ? $companyBasic->mobile : $companyBasic->phone ;
        return Style::view('mobile.company.welcome',compact('companyBasic','userInfo','resumeSendCount','endtime','integral'));
    }


    /**
     * 功能：计算两个日期相差 年 月 日
     * @param date   $endtime 截止日期日期
     * @return array(int,'单位')
     */
    protected function timeDiff($endtime){
        $starttime = time();
        $endtime = strtotime($endtime);

        $timediff = $endtime - $starttime;
        $days = intval( $timediff / 86400 );
        $remain = $timediff % 86400;
        $hours = intval( $remain / 3600 );
        $remain = $remain % 3600;
        $mins = intval( $remain / 60 );
        $secs = $remain % 60;
        if($days>0) return array($days,'天');
        if($hours>0) return array($hours,'小时');
        if($mins>0) return array($mins,'分种');
        return array($secs,'秒');
    }
}